const uniRequest = {
  request(method, url, data = {}) {
    return new Promise((resolve, reject) => {
      uni.request({
        method: method || 'POST',
        url: url,
        timeout: 1000 * 60,
        data: data,
        header: {
          // 根据后端需求填写token
          'token': uni.getStorageSync('token') || ''
        },
        success: (res) => {
          if (res.statusCode == 200) {
            if (res.data.code == -1) {
              uni.showToast({
                title: '请登录',
                icon: 'none'
              })
            }
            let data = res.data;
            if (!res.data.code) {
              data.code = 200;
            }
            resolve(data);
          } else {
            if (res.data.code) {
              resolve(res.data)
            } else {
              resolve({code: res.statusCode || 1, msg: res.data.message || '请求失败'});
            }
          }
        },
        fail: (e) => {
          resolve({code: 504, msg: '请求超时'})
        }
      });
    });
  },
  get(url, params) {
    return this.request('GET', url, params)
  },
  put(url, params) {
    return this.request('PUT', url, params)
  },
  post(url, data) {
    return this.request('POST', url, data)
  },
  deleted(url, data) {
    return this.request('DELETE', url, data)
  }
}

export default uniRequest;
